﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;

namespace mal.balades.Business.Web
{
    public class AudienceTrackingHelper
    {
        private const string ConfigKey = "AudienceTrackingExcludedIps";
        private const string CookieKey = "AudienceTrackingDisabled";


        private AudienceTrackingHelper()
        {

        }

        public static AudienceTrackingHelper Instance
        {
            get
            {
                return new AudienceTrackingHelper();
            }
        }


        public bool TrackingEnabled
        {
            get
            {
                try
                {
                    if (IsCookieExist || IsExcludedIp)
                        return false;
                    else
                        return true;
                }
                catch
                {

                }

                return true;
            }
        }

        private bool IsExcludedIp
        {
            get
            {
                string excludesIps = ConfigurationManager.AppSettings[ConfigKey];
                string[] excludesIpsArray = excludesIps.Split(";".ToCharArray());

                string visitorIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

                if (excludesIpsArray.Contains(visitorIP))
                    return true;
                else
                    return false;
            }
        }

        private bool IsCookieExist
        {
            get
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies[CookieKey];
 
                if (cookie!=null)
                    return true;
                else
                    return false;
            }
        }
    }
}